package com.artpri.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.artpri.dao.GoodAttributeDao;
import com.artpri.model.GoodAttribute;
import com.artpri.model.vo.GoodAttributeVo;
import com.artpri.util.SegmentString;

/**
 * @author ZhangSi
 */
public class GoodAttributeDaoImpl extends HibernateDaoImpl<GoodAttribute>
		implements GoodAttributeDao {

	@Override
	public Map<String, List<String>> getGoodAttributeByCategory(int gcId) {
		
		Map<String, List<String>> map = null;
		List<GoodAttribute>  list = find("from GoodAttribute a where a.gcId=?0 and a.attrInputType=1 and a.attrShow=1", gcId);
		
		if (list != null && list.size() != 0) {
			map = new HashMap<>();
			for (Iterator<GoodAttribute> it = list.iterator(); it.hasNext();) {
				GoodAttribute attr = (GoodAttribute) it.next();
				String value = attr.getAttrValues();
				List<String> alist = SegmentString.tokenizerString(value, "\r\n");
				map.put(attr.getAttrName(), alist);
			}
		}
		
		return map;
	}

	@Override
	public List<GoodAttributeVo> getGoodAttributeVoByCategory(int gcId) {
		List<GoodAttributeVo> list = null;
		
		List<GoodAttribute> list2 = find("from GoodAttribute a where a.gcId=?0 and a.attrInputType=1 and a.attrShow=1", gcId);
		
		if (list2 != null && list2.size() != 0) {
			list = new ArrayList<>();
			
			for (Iterator<GoodAttribute> it = list2.iterator(); it.hasNext();) {
				GoodAttribute attr = (GoodAttribute) it.next();
				GoodAttributeVo vo = new GoodAttributeVo();
				vo.setAttrId(attr.getAttrId());
				vo.setAttrIndex(attr.getAttrIndex());
				vo.setAttrInputType(attr.getAttrInputType());
				vo.setAttrName(attr.getAttrName());
				vo.setGcId(attr.getGcId());
				vo.setAttrShow(attr.getAttrShow());
				vo.setAttrValues(SegmentString.tokenizerString(attr.getAttrValues(), "\r\n"));
				list.add(vo);
			}
		}
		
		return list;
	}

}
